package br.com.felix.dbm.ui.components;

import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.GroupLayout.Group;

import br.com.felix.dbm.controller.TableController;
import br.com.felix.dbm.database.DataModel;
import br.com.felix.dbm.database.PrimaryKey;
import br.com.felix.dbm.database.Table;

public class ForeignKeyDialog extends JFrame {

	private static final long serialVersionUID = 8805154588150015311L;
	private JList lstPks; 
	private JScrollPane jspPks;
	private TableController tableController;
	
	public ForeignKeyDialog(TableController tableToAddForeignKey){
		super("Foreign Key Selection");
		this.tableController = tableToAddForeignKey;
		configureDefaults();
		
		Table model = tableController.getModel();
		DataModel dataModel = model.getDataModel();
		List<PrimaryKey> lst = dataModel.getPrimaryKeys();
		boolean primaryKeysWereDefined = lst.size()>0;
		if(primaryKeysWereDefined){
			displayPrimaryKeysForChoosing(lst);
		}
		else{
			disposeWhenThereAreNoPrimaryKeys();
		}
	}

	
	
	private void displayPrimaryKeysForChoosing(List<PrimaryKey> lst) {
		DefaultListModel lm = new DefaultListModel();
		for(PrimaryKey pk:lst)lm.addElement(pk);
		lstPks = new JList(lm);
		lstPks.addMouseListener(new ChoosePrimaryKeyMouseListener());
		layoutComponents();
	}

	
	
	private void disposeWhenThereAreNoPrimaryKeys() {
		JOptionPane.showMessageDialog(this, "No primary key was defined.","Attention!",JOptionPane.INFORMATION_MESSAGE);
		addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				dispose();
			}
			@Override
			public void focusGained(FocusEvent e) {
				dispose();
			}
		});
	}

	
	
	private void layoutComponents() {
		jspPks = new JScrollPane(lstPks);
		JPanel jpa = new JPanel();
		GroupLayout layout = new GroupLayout(jpa);
		JLabel lblTitulo = new JLabel("Please choose a primary key:");
		Group verticalGroup = layout.createSequentialGroup()
			.addComponent(lblTitulo)
			.addComponent(jspPks);
		Group horizontalGroup = layout.createParallelGroup()
			.addComponent(lblTitulo)
			.addComponent(jspPks);
		layout.setHorizontalGroup(horizontalGroup);
		layout.setVerticalGroup(verticalGroup);
		jpa.setLayout(layout);
				
		getContentPane().add(jpa);
	}

	
	
	private void configureDefaults() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setSize(new Dimension(800,600));
	}
	
	
	class ChoosePrimaryKeyMouseListener extends MouseAdapter{
		@Override
		public void mouseReleased(MouseEvent e) {
			if(e.getClickCount()>=2){
				primaryKeyWasSelected((PrimaryKey)lstPks.getSelectedValue());
			}
		}
	}


	
	public void primaryKeyWasSelected(PrimaryKey selectedPrimaryKey) {
		tableController.addForeignKey(selectedPrimaryKey);
		tableController.updateView();
		dispose();
	}

}
